def div(x):
    d = set()
    for i in range(1,int(x**0.5)+1):
        if x%i==0:
            d.add(i)
            d.add(x//i)
    return sorted(d)
while True:
    l = []
    m = []
    a = int(input())
    s = str(a)
    for i in range(len(s)-1):
        for j in range(i,len(s)):
            l.append(int(s[j-i:j+1]))
    l.append(int(s))
    for i in range(len(l)):
        if len(div(l[i]))<=2:
            m.append(l[i])
    if len(m)>0:
        print(max(m))
    if len(m)==0:
        print(0)
